package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.BusiMplstp;
import com.ruoyi.net.domain.BusiMtnChannelCrossListVo;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.BusiMtnChannelCross;
import com.ruoyi.net.service.IBusiMtnChannelCrossService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务-MTN Channel crossController
 * 
 * @author tony
 * @date 2024-12-28
 */
@Api(tags = "业务-MTN Channel cross")
@RestController
@RequestMapping("/busi/mtn/channel/cross")
public class BusiMtnChannelCrossController extends BaseController
{
    @Autowired
    private IBusiMtnChannelCrossService busiMtnChannelCrossService;
    @Autowired
    private DeviceManager deviceManager;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private INetElementService netElementService;

    /**
     * 查询业务-MTN Channel cross列表
     */
    @ApiOperation(value = "查询网元-业务-MTN Channel cross列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:cross:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiMtnChannelCross busiMtnChannelCross)
    {
        startPage();
        busiMtnChannelCross.setOrder("desc");
        List<BusiMtnChannelCrossListVo> list = busiMtnChannelCrossService.selectBusiMtnChannelCrossList(busiMtnChannelCross);
        return getDataTable(list);
    }

    /**
     * 导出业务-MTN Channel cross列表
     */
    @ApiOperation(value = "导出业务-MTN Channel cross列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:cross:export')")
    @Log(title = "业务-MTN Channel cross", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiMtnChannelCross busiMtnChannelCross)
    {
        List<BusiMtnChannelCrossListVo> list = busiMtnChannelCrossService.selectBusiMtnChannelCrossList(busiMtnChannelCross);
        ExcelUtil<BusiMtnChannelCrossListVo> util = new ExcelUtil<BusiMtnChannelCrossListVo>(BusiMtnChannelCrossListVo.class);
        util.exportExcel(response, list, "业务-MTN Channel cross数据");
    }

    /**
     * 获取业务-MTN Channel cross详细信息
     */
    @ApiOperation(value = "获取业务-MTN Channel cross详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:cross:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(busiMtnChannelCrossService.selectBusiMtnChannelCrossById(id));
    }

    /**
     * 新增业务-MTN Channel cross
     */
    @ApiOperation(value = "新增业务-MTN Channel cross", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:cross:add')")
    @Log(title = "业务-MTN Channel cross", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiMtnChannelCross busiMtnChannelCross)
    {
        return toAjax(busiMtnChannelCrossService.insertBusiMtnChannelCross(busiMtnChannelCross));
    }

    /**
     * 修改业务-MTN Channel cross
     */
    @ApiOperation(value = "修改业务-MTN Channel cross", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:cross:edit')")
    @Log(title = "业务-MTN Channel cross", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiMtnChannelCross busiMtnChannelCross)
    {
        return toAjax(busiMtnChannelCrossService.updateBusiMtnChannelCross(busiMtnChannelCross));
    }

    /**
     * 删除业务-MTN Channel cross
     */
    @ApiOperation(value = "删除业务-MTN Channel cross", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:cross:remove')")
    @Log(title = "业务-MTN Channel cross", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(busiMtnChannelCrossService.deleteBusiMtnChannelCrossByIds(ids));
    }
    /**
     * 激活设备
     *
     * @param id
     */
    @ApiOperation(value = "激活设备", httpMethod = "GET")
    @GetMapping(value = "/activate/{id}")
    @Log(title = "激活设备", businessType = BusinessType.UPDATE)
    public AjaxResult activateDevice(@PathVariable("id") String id) {
        BusiMtnChannelCross busiMtnChannelCross = new BusiMtnChannelCross();
        busiMtnChannelCross.setId(id);
        List<BusiMtnChannelCrossListVo> list = busiMtnChannelCrossService.selectBusiMtnChannelCrossList(busiMtnChannelCross);
        if (!CollectionUtils.isEmpty(list) && list.size()>0){
            list.get(0).setStatus("1");
            if (sendXml(list.get(0))){
                return toAjax(busiMtnChannelCrossService.updateBusiMtnChannelCross(list.get(0)));
            }else {
                return AjaxResult.error("激活设备失败");
            }
        }else {
            return toAjax(0);
        }

    }

    /**
     * 去激活设备
     *
     * @param id
     */
    @ApiOperation(value = "去激活设备", httpMethod = "GET")
    @Log(title = "去激活设备", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/unactivate/{id}")
    public AjaxResult unactivateDevice(@PathVariable("id") String id) {
        BusiMtnChannelCross busiMtnChannelCross = new BusiMtnChannelCross();
        busiMtnChannelCross.setId(id);
        List<BusiMtnChannelCrossListVo> list = busiMtnChannelCrossService.selectBusiMtnChannelCrossList(busiMtnChannelCross);
        if (!CollectionUtils.isEmpty(list) && list.size()>0){
            list.get(0).setStatus("0");
            if (sendXml(list.get(0))){
                return toAjax(busiMtnChannelCrossService.updateBusiMtnChannelCross(list.get(0)));
            }else {
                return AjaxResult.error("去激活设备失败");
            }
        }else {
            return toAjax(0);
        }
    }

      /*<flexexc xmlns="http://fiberhome.com/fhnw/yang/transmissionservice/fos-flexeconnection">
    <cross-connects>
      <cross-connect>
        <cross-connect-name>flexecross_67490</cross-connect-name>
        <endpoint1-name>flexe-tunnel4</endpoint1-name>
        <endpoint2-name>flexe-tunnel5</endpoint2-name>
      </cross-connect>
    </cross-connects>
  </flexexc>*/

    /**
     * 发送xml到设备
     *
     * @param entity
     */
    private boolean sendXml(BusiMtnChannelCrossListVo entity) {
        boolean isSend = true;
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getStartElement());
            deviceManager.getDevice(netElement.getIp());
            String xml = "<flexexc xmlns=\"http://fiberhome.com/fhnw/yang/transmissionservice/fos-flexeconnection\">\n" +
                        "    <cross-connects>\n" +
                        "      <cross-connect>\n" +
                        "        <cross-connect-name>"+entity.getCrossName()+"</cross-connect-name>\n" +
                        "        <endpoint1-name>"+entity.getStartPortName()+"</endpoint1-name>\n" +
                        "        <endpoint2-name>"+entity.getEndPortName()+"</endpoint2-name>\n" +
                        "      </cross-connect>\n" +
                        "    </cross-connects>\n" +
                        "  </flexexc>";
            System.out.println(xml);
            isSend = deviceManager.editConfig(xml, "merge");
        }
        return isSend;
    }
}
